<?php
/*
 * @Author: lokei
 * @Date: 2022-08-16 13:16:33
 * @LastEditors: lokei
 * @LastEditTime: 2022-08-17 00:23:38
 * @Description: 
 */

namespace App\Http\Controllers\Pay;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Platform\ConfModel;
use Illuminate\Http\Request;

use GuzzleHttp;

class WxController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    //
    public function pay(Request $request)
    {
        $conf = ConfModel::first();
        $http = new GuzzleHttp\Client;
        $params = [
            'json' => [
                'mchid' => $conf->mchid,
                'out_trade_no' => date('Ymd') . substr(implode("", array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8),
                'appid' => $conf->miniapp_appid,
                'description' => '微信支付API',
                'notify_url' => 'https://open.lokei.cn/pay/wx/notify',
                'amount' => [
                    'total' => 1,
                    'currency' => 'CNY'
                ],
                'payer' => [
                    'openid' => $request->input('openid')
                ]
            ],
            'headers' => ['Accept' => 'application/json']
        ];
        $response = $http->post('https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi', $params);
        $result = json_decode($response->getBody(), true);
        return ResultTool::success();
    }
}
